package edcom.server;

import edcom.client.Student;

public class AddStudentView {
	
	/*
	 * class edcom.AddStudentView 
	 * 
	 *  projected class functionalities: 
	 *  
	 *  - handles the form data
	 *  - invokes AddStudentController to validate well-formedness of string 
	 *  	inputs
	 *  - functions:
	 *  	requestAddStudent()
	 *  	redirect()
	 *  	showErrorMessage()
	 *  
	 *  implemented class functionalities:
	 *  
	 *  [2011JAN29]
	 *  - requestAddStudent returns a string array of error messages, easily 
	 *  	accessible at higher level via Student class (integer) constants;
	 *  	this incorporates "showErrorMessage" function
	 *  
	 *  	ex:
	 *  		AddStudentView asv = new AddStudentView();
	 *  		String[] msgs = asv.requestAddStudent( ... );
	 *  		
	 *  		// you can access the error message for the registration form's
	 *  		// password field through this:
	 *  		out.println(msgs[Student.password]);
	 *  
	 *  		// access what happened to SQL after table insertion is tried
	 *  		out.println(msgs[Student.sqlStatus]);
	 *  
	 */

	private AddStudentController stdctlr;
	
	public String[] errorMessages;

	public AddStudentView(){
		stdctlr = new AddStudentController();
		errorMessages = new String[8];
		errorMessages[Student.name] = "";
		errorMessages[Student.studentNumber] = "";
		errorMessages[Student.username] = "";
		errorMessages[Student.password] = "";
		errorMessages[Student.email] = "";
		errorMessages[Student.major] = "";
		errorMessages[Student.sqlStatus] = "Not registered.";
		errorMessages[Student.generalError] = "";
	}
	
	public String[] getErrorMessages()
	{
		return errorMessages;
	}
	
	public String[] requestAddStudent(String name, String snum, 
			String username, String password, String email, String major) 
			throws Exception
	{
		boolean CHECK_SUCCESS = true;
		
		try{
			stdctlr.setEmail(email);
		}catch(MalformedStringException e){
			errorMessages[Student.email] = e.getMessage();
			CHECK_SUCCESS = false; 
		}
		
		try{
			stdctlr.setMajor(major);
		}catch(MalformedStringException e){
			errorMessages[Student.major] = e.getMessage();
			CHECK_SUCCESS = false;
		}
		
		try{
			stdctlr.setName(name);
		}catch(MalformedStringException e){
			errorMessages[Student.name] = e.getMessage();
			CHECK_SUCCESS = false;
		}
		
		try{
			stdctlr.setPassword(password);
		}catch(MalformedStringException e){
			errorMessages[Student.password] = e.getMessage();
			CHECK_SUCCESS = false;
		}
		
		try{
			stdctlr.setSnum(snum);
		}catch(MalformedStringException e){
			errorMessages[Student.studentNumber] =  e.getMessage();
			CHECK_SUCCESS = false;
		}
		
		try{
			stdctlr.setUsername(username);
		}catch(MalformedStringException e){
			errorMessages[Student.username] =  e.getMessage();
			CHECK_SUCCESS = false;
		}
		
		if(CHECK_SUCCESS)
		{
			try {
				stdctlr.addStudent();
				errorMessages[Student.sqlStatus] = "";
			}catch(SQLInsertException e){
				errorMessages[Student.sqlStatus] = e.getMessage();
			}catch(Exception e){
				errorMessages[Student.generalError] = "ERROR: " + e.getMessage();
			}
		}
		
		return errorMessages;
	}
	
}
